package com.itheima.accountingdb.controller;


import com.itheima.accountingdb.pojo.*;
import com.itheima.accountingdb.service.IUserService;
import com.itheima.accountingdb.utils.Md5Util;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表，存储用户基本信息 前端控制器
 * </p>
 *
 * @author pww
 * @since 2025-03-06
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户管理相关接口")
public class UserController {

    private final IUserService userService;

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Operation(summary = "新增用户", deprecated = false)
    @PostMapping("/add")
    public Result add(User user) {
        //新增用户
        if (user == null) {
            return Result.error("新增失败");
        }
        //获取用户密码 并对用户密码进行md5加密
        String password = user.getPassword();
        System.out.println("password:"+password);
        String md5PassWord = Md5Util.getMD5String(password);
        System.out.println("md5PassWord:"+md5PassWord);
        user.setPassword(md5PassWord);
        boolean rows = userService.save(user);
        System.out.println(rows);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", deprecated = false)
    public Result delete(@PathVariable Long id){
        boolean flag = userService.removeById(id);
        if (flag){
            return Result.success();
        }
        return Result.error("删除失败");
    }

    @GetMapping("/list")
    @Operation(summary = "查询用户列表", deprecated = false)
    private PageBean<User> queryUserPage(UserQuery user){
        return userService.queryUserPage(user);
    }
}
